/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.kernel;

import org.openaecfm.model.kernel.Root;
import org.openaecfm.model.kernel.SimplePropertyTemplate;
import org.openaecfm.model.kernel.SimplePropertyTemplateTypeEnum;
import org.openaecfm.model.property.PropertyEnumeration;
import org.openaecfm.model.utility.StateEnum;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class SimplePropertyTemplateIO extends
		AbstractEntityInstanceInputImpl<SimplePropertyTemplate> implements
		IEntityInstanceOutput<SimplePropertyTemplate> {

	private IEntityInstanceUtilOutput<Root> utilOutput;
	private IEntityInstanceUtilInput<Root> utilInput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Root> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<Root> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(SimplePropertyTemplate pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCSIMPLEPROPERTYTEMPLATE", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set TemplateType
		if (pojo.getTemplateType() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EnumerationParameter(pojo.getTemplateType()));
		}

		// Set PrimaryMeasureType
		if (pojo.getPrimaryMeasureType() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getPrimaryMeasureType(),
					StringParameter.UTF_8));
		}

		// Set SecondaryMeasureType
		if (pojo.getSecondaryMeasureType() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getSecondaryMeasureType(),
					StringParameter.UTF_8));
		}

		// Set Enumerators
		if (pojo.getEnumerators() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getEnumerators())));
		}

		// Set PrimaryUnit
		if (pojo.getPrimaryUnit() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getPrimaryUnit())));
		}

		// Set SecondaryUnit
		if (pojo.getSecondaryUnit() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getSecondaryUnit())));
		}

		// Set Expression
		if (pojo.getExpression() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getExpression(),
					StringParameter.UTF_8));
		}

		// Set AccessState
		if (pojo.getAccessState() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EnumerationParameter(pojo.getAccessState()));
		}

		return ei.toString();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			SimplePropertyTemplate pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Enumerators
		Parameter enumerators = sei.getParameter(7);
		if (enumerators.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) enumerators;
			pojo.setEnumerators((PropertyEnumeration) appDataManager
					.getObject(e.toInstanceName()));
		}

		// Set PrimaryUnit
		Parameter primaryUnit = sei.getParameter(8);
		if (primaryUnit.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) primaryUnit;
			pojo.setPrimaryUnit(appDataManager.getObject(e.toInstanceName()));
		}

		// Set SecondaryUnit
		Parameter secondaryUnit = sei.getParameter(9);
		if (secondaryUnit.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) secondaryUnit;
			pojo.setSecondaryUnit(appDataManager.getObject(e.toInstanceName()));
		}
	}

	@Override
	protected SimplePropertyTemplate doCreateObject() {
		return new SimplePropertyTemplate();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			SimplePropertyTemplate pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set TemplateType
		Parameter templateType = sei.getParameter(4);
		if (templateType.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) templateType;
			pojo.setTemplateType(SimplePropertyTemplateTypeEnum.valueOf(e
					.toEnumString()));
		}

		// Set PrimaryMeasureType
		Parameter primaryMeasureType = sei.getParameter(5);
		if (primaryMeasureType.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) primaryMeasureType;
			pojo.setPrimaryMeasureType(s.toUTF8());
		}

		// Set SecondaryMeasureType
		Parameter secondaryMeasureType = sei.getParameter(6);
		if (secondaryMeasureType.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) secondaryMeasureType;
			pojo.setSecondaryMeasureType(s.toUTF8());
		}

		// Set Expression
		Parameter expression = sei.getParameter(10);
		if (expression.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) expression;
			pojo.setExpression(s.toUTF8());
		}

		// Set AccessState
		Parameter accessState = sei.getParameter(11);
		if (accessState.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) accessState;
			pojo.setAccessState(StateEnum.valueOf(e.toEnumString()));
		}
	}
}
